﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ComponentModel;

namespace InCall_Lock
{
    public static class Log
    {
        //public string logFile = "sleeptightlog.txt";
        [DefaultValue(true)]
        public static bool logEnabled { get; set; }
        public static string logFile { get; set; }
        public static int logSize { get; set; }
        public static void Start (string fileName, int fileSize)
        {
            logFile = fileName;
            logSize = fileSize;
            if (logEnabled)
            {
                if (!File.Exists(logFile))
                {
                    FileStream fs = File.Create(logFile);
                    fs.Close();
                }
                else
                {
                    if (logSize != 0)
                    {
                        if (logSize > 0)
                        {
                            FileInfo fi = new FileInfo(logFile);
                            if (fi.Length >= logSize)
                            {
                                //Reset file
                                Reset();
                            }
                        }
                        else
                        {
                            Reset();
                        }
                    }
                }
                StreamWriter sw = File.AppendText(logFile);
                sw.WriteLine("---------------------");
                sw.Flush();
                sw.Close();
            }
            else
                return;
        }
        public static void Append(string entry)
        {
            if (logEnabled)
            {
                // Code here
                //DateTime dt = DateTime.Now;
                //string dtString = dt.ToShortTimeString();
                StreamWriter sw = File.AppendText(logFile);
                //StreamWriter sw1 = File.
                sw.WriteLine(DateTime.Now + ", " + entry);
                sw.Flush();
                sw.Close();
                //StreamWriter sw = new StreamWriter(logFile);
                //sw.WriteLine(DateTime.Now + ", " + entry);
                //sw.Close(); 
            }
            else
                return;
        }
        public static void Reset()
        {
            if (logEnabled)
            {
                //Code to reset 
                File.Delete(logFile);
                FileStream fs = File.Create(logFile);
                fs.Close();
            }
            else
                return;
        }
        public static void Overwrite(string entry)
        {
            if (logEnabled)
            {
                //Code to start writing from beginning
                throw new Exception("This method is not yet implemented");
            }
            else
                return;
        }
    }
}
